using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProfileLayer : MonoBehaviour
{
	public UIWidget inputControlWidget;

	public SkillDisplayButton skillDisplayButtonPrefab;

	public UITable table;

	public Vector3 localOffsetButtons = Vector3.zero;

	public GameObject winningsCaller;

	public GameObject HomePageButton;
	protected List<SkillDisplayButton> skillButtons = new List<SkillDisplayButton>();

	public List<SkillDisplayButton> profileSkillButtons
	{
		get
		{
			return skillButtons;
		}
	}

	private void Awake()
	{
		if (winningsCaller != null)
		{
			winningsCaller.SetActive(true);
		}
	}

	public void OnEnable()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		tableTennisMainNavigation.SetHeader(TableTennisMainNavigation.HeaderType.CoinsCashEnergy);
		Vector3 position = inputControlWidget.worldCorners[2];
		float x = UICamera.mainCamera.WorldToViewportPoint(position).x;
		Debug.Log("Main navigation " + (tableTennisMainNavigation != null));
		tableTennisMainNavigation.characterView.ShowCharacter(0.5f);
		tableTennisMainNavigation.characterView.ShowView(CharacterDisplayView.ViewType.ProfileView, false);
		Init();
		HomePageButton.SetActive(true);

	}

	public void OnDisable()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		tableTennisMainNavigation.characterView.Hide();
		HomePageButton.SetActive(false);
	}

	public void Init()
	{
		int num = 0;
		Vector3 localPosition = skillDisplayButtonPrefab.transform.localPosition;
		PlayerStats playerStats = GGPlayerSettings.instance.statsManager.playerStats;
		UITools.SetActive(skillDisplayButtonPrefab, false);
		PlayerStatsManager.SkillType[] displaySkills = playerStats.displaySkills;
		foreach (PlayerStatsManager.SkillType skillType in displaySkills)
		{
			if (num >= skillButtons.Count)
			{
				Transform parent = skillDisplayButtonPrefab.transform.parent;
				SkillDisplayButton component = NGUITools.AddChild(parent.gameObject, skillDisplayButtonPrefab.gameObject).GetComponent<SkillDisplayButton>();
				skillButtons.Add(component);
			}
			SkillDisplayButton skillDisplayButton = skillButtons[num];
			skillDisplayButton.transform.localPosition = localPosition + localOffsetButtons * num;
			skillDisplayButton.gameObject.name = GGFormat.IndexName(num);
			skillDisplayButton.gameObject.SetActive(true);
			skillDisplayButton.Init(skillType);
			num++;
		}
		ScrollToTop();
	}

	private void ScrollToTop()
	{
		StopAllCoroutines();
		StartCoroutine(DoScrollToTop());
	}

	public void GoToMenu()
	{
		BehaviourSingleton<ScreenNavigation>.instance.LoadMainMultiplayer(false);
	}

	public void SeeDetectedServers()
	{
		BehaviourSingleton<ScreenNavigation>.instance.LoadServerDetectionDemo();
	}

	private IEnumerator DoScrollToTop()
	{
		if (!(table == null))
		{
			for (int i = 0; i < 4; i++)
			{
				UITools.AlignToTopOnScroll(skillButtons[0].GetComponent<UIWidget>(), table.transform.parent.GetComponent<UIScrollView>(), -1f);
				yield return null;
			}
		}
	}
}
